package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先要得到博客得列表，这个列表在BlogDao中（有一个查询博客列表得方法，放到了一个链表中）
        String blogId = req.getParameter("blogId");
        BlogDao blogDao = new BlogDao();
        if (blogId == null) {

            try {
                List<Blog> blogList = blogDao.getBlogs();
                String respJson = objectMapper.writeValueAsString(blogList);
                //然后就把博客列表按照json格式返回给前端
                System.out.println("respJson" + respJson);
                resp.setContentType("application/json; charset=utf8");
                resp.getWriter().write(respJson);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //然后就把博客列表按照json格式返回给前端
        }else{
            try {
               Blog blog =  blogDao.getBlog(Integer.parseInt(blogId));
                String respJson = objectMapper.writeValueAsString(blog);
                System.out.println("respJson" + respJson);
                resp.setContentType("application/json; charset=utf8");
                resp.getWriter().write(respJson);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title==null||title.length()==0||content==null||content.length()==0){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前传过来的标题或正文为空");
            return;
        }
        HttpSession session = req.getSession(false);
        if(session==null){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户没登陆");
            return;
        }
        User user = (User) session.getAttribute("user");
        if(user==null){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户没登陆");
            return;
        }
        BlogDao blogDao = new BlogDao();
        Blog blog = new Blog();
        blog.setContent(content);
        blog.setTitle(title);
        blog.setUseId(user.getUserId());
        try {
            blogDao.insert(blog);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.sendRedirect("blog_list.html");

    }
}
